package com.pen.changgou.goods.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;
import java.util.Map;

import com.pen.changgou.goods.entity.Goods;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.pen.changgou.goods.entity.Spu;
import com.pen.changgou.goods.service.SpuService;
import com.pen.changgou.common.core.utils.R;

import java.util.Arrays;

/**
 * @author yaodao
 * @email lfy@pen.changgou.com
 * @date 2020-11-29 17:35:47
 */
@Api(tags = "管理")
@RestController
@RequestMapping("/spu")
public class SpuController {
    @Autowired
    private SpuService spuService;

    /**
     * 上架
     *
     * @param id
     * @return
     */
    @PutMapping("up/{id}")
    public R up(@PathVariable Long id) {
        return spuService.up(id);
    }

    /**
     * 下架
     *
     * @param id
     * @return
     */
    @PutMapping("down/{id}")
    public R down(@PathVariable Long id) {
        return spuService.down(id);
    }

    /**
     * 上架
     *
     * @param ids
     * @return
     */
    @PutMapping("upBatch")
    public R upBatch(@PathVariable Long[] ids) {
        return spuService.upBatch(ids);
    }

    /**
     * 下架
     *
     * @param ids
     * @return
     */
    @PutMapping("down")
    public R downBatch(@PathVariable Long[] ids) {
        return spuService.downBatch(ids);
    }

    /**
     * 商品审核
     *
     * @param id
     * @return
     */
    @PostMapping("audit/{id}")
    public R audit(@PathVariable Long id) {
        return spuService.audit(id);
    }

    /**
     * 商品新增
     *
     * @param goods
     * @return
     */
    @PostMapping("saveGoods")
    public R saveGoods(@RequestBody Goods goods) {
        return spuService.saveGoods(goods);
    }

    /**
     * 商品查找
     *
     * @param id
     * @return
     */
    @PostMapping("goods/{id}")
    public R<Goods> findGoodsById(@PathVariable Long id) {
        return spuService.findGoodsById(id);
    }

    /**
     * 通过ID查询菜单的详细信息
     *
     * @param id 菜单ID
     * @return 菜单详细信息
     */
    @ApiOperation("获取详情")
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id) {
        return R.ok(spuService.getById(id));
    }

    /**
     * 列表
     */
    @ApiOperation("分页查询(排序)")
    @GetMapping("/page")
    public R<Page> pge(Page page, Spu spu) {
        return R.ok(spuService.page(page, Wrappers.query(spu)));

    }

    @ApiOperation("分页查询(排序)")
    @GetMapping("/list")
    public R<List<Spu>> list(Map<String, Object> param) {
        return R.ok(spuService.list(new QueryWrapper<>()));

    }

    /**
     * 信息
     */
    @ApiOperation("详情查询")
    @GetMapping("/info/{id}")
    public R<Spu> info(@PathVariable("id") Long id) {
        Spu spu = spuService.getById(id);

        return R.ok(spu);
    }

    /**
     * 保存
     */
    @ApiOperation("保存")
    @PostMapping("/save")

    public R<Object> save(@RequestBody Spu spu) {
        spuService.save(spu);

        return R.ok(null);
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @PostMapping("/update")
    public R<Object> update(@RequestBody Spu
                                    spu) {
        spuService.updateById(spu);

        return R.ok(null);
    }

    /**
     * 删除
     */
    @ApiOperation("删除")
    @PostMapping("/delete")
    public R<Object> delete(@RequestBody Long[] ids) {
        spuService.removeByIds(Arrays.asList(ids));

        return R.ok(null);
    }

}
